package com.seeyon.apps.ddkk.manager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.ddkk.dao.QiangsDao;
import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.dao.SyncOrgDao;
import com.seeyon.apps.kk.manager.BaseManager;
import com.seeyon.apps.kk.manager.FormDataBaseManager;
import com.seeyon.apps.kk.util.JdbcAgentUtil;
import com.seeyon.apps.kk.util.RandomCode;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.cap4.form.bean.FormFieldBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgLevel;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.bo.V3xOrgPrincipal;
import com.seeyon.ctp.organization.bo.V3xOrgRelationship;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.organization.manager.OrgManagerDirect;
import com.seeyon.ctp.util.Strings;
import com.seeyon.kk.utils.CoreUtils;

public class DDKkDistributorManger extends BaseManager {
	private static final Log log = LogFactory.getLog(DDKkDistributorManger.class);
	private String CodeForDd;
	private String CodeForCpx;
	private String CodeForPaiban;
	private QiangsDao qiangsDao;
	private QiangsDDManager ddupdateHcc;
	private FormApi4Cap4 formApi4Cap4;
	private FormDataBaseManager kkBaseFormManager;
	
	public void setFormApi4Cap4(FormApi4Cap4 formApi4Cap4) {
		this.formApi4Cap4 = formApi4Cap4;
	}

	public void setKkBaseFormManager(FormDataBaseManager kkBaseFormManager) {
		this.kkBaseFormManager = kkBaseFormManager;
	}

	public void setQiangsDao(QiangsDao qiangsDao) {
		this.qiangsDao = qiangsDao;
	}
	
	public QiangsDDManager getDdupdateHcc() {
		return ddupdateHcc;
	}

	public void setDdupdateHcc(QiangsDDManager ddupdateHcc) {
		this.ddupdateHcc = ddupdateHcc;
	}

	public String getCodeForPaiban() {
		return CodeForPaiban;
	}

	public void setCodeForPaiban(String codeForPaiban) {
		CodeForPaiban = codeForPaiban;
	}

	public String getCodeForDd() {
		return CodeForDd;
	}

	public void setCodeForDd(String codeForDd) {
		CodeForDd = codeForDd;
	}

	public String getCodeForCpx() {
		return CodeForCpx;
	}

	public void setCodeForCpx(String codeForCpx) {
		CodeForCpx = codeForCpx;
	}

	private SyncOrgDao syncOrgDao;
	
	public SyncOrgDao getSyncOrgDao() {
		return syncOrgDao;
	}

	public void setSyncOrgDao(SyncOrgDao syncOrgDao) {
		this.syncOrgDao = syncOrgDao;
	}

/**
 * 创建经销商发起前校验
 * @param formBean
 * @param formDataMasterBean
 * @param Ddlc
 * @param formdata
 * @return
 * @throws Exception
 */
	public Map<String,Object> StartJiaoyan(FormBean formBean,FormDataMasterBean formDataMasterBean) throws Exception{
		FormTableBean Ddlc = formBean.getMasterTableBean();
		Map<String, Object> resultMap=new HashMap<String, Object>();
		String DDname = getString(formDataMasterBean, formBean, "经销商名称");
		String BU = getString(formDataMasterBean, formBean, "产品线");
		
		Map params = new HashMap();
		params.put("name", DDname);
//		params.put("bu", BU);
		params.put("masterId", formDataMasterBean.getId());
		boolean notIn = qiangsDao.checkAgentNotInFlow(formBean, params );
		if(notIn) {
			/**流程中不能发起*/
			resultMap.put("result",false);
			resultMap.put("msg","流程中已经存在该经销商,不能发起;请联系渠道运营部门!");
			return resultMap;
		} else {
			FormBean productBean = formApi4Cap4.getFormByFormCode(CodeForCpx);
			String abridge = qiangsDao.getProductAbridge(productBean, BU);
			FormBean simsBean = formApi4Cap4.getFormByFormCode(CodeForDd);
			
			Map simsMap = new HashMap();
			simsMap.put("name", DDname);
			List<Map> simsList = qiangsDao.getSimsByMap(simsBean, simsMap);
			if (CollectionUtils.isNotEmpty(simsList)) {
				FormFieldBean field = formBean.getFieldBeanByDisplay("经销商等级");
				Object toObj = formDataMasterBean.getFieldValue(field.getName());
				Object[] displayObj = field .getDisplayValue(toObj, true);
				String level = displayObj[1].toString();
				for (Map<String, Object> agent : simsList) {
					if ("红色预警".equals(MapUtils.getString(agent, "dd"))) {
						resultMap.put("result",false);
						resultMap.put("msg","该公司为严重红色预警经销商，不能申请；如有任何问题，请联系合规部。");
						return resultMap;
					}
				}
				for (Map<String, Object> agent : simsList) {
					log.info("##强生客开:  abridge="+abridge+"--------------------bu=" + MapUtils.getString(agent, "底表bu"));
					if(abridge.equals(MapUtils.getString(agent, "底表bu"))){
						if("一级经销商".equals(level)){
							if("一级".equals(MapUtils.getString(agent, "level").trim())
									&& "经销商".equals(MapUtils.getString(agent, "clname"))){//
								resultMap.put("result",false);
								resultMap.put("msg","该公司已完成注册，无需提交申请。");
								return resultMap;
							}
						} else if("非一级经销商".equals(level)){
							if(!"一级".equals(MapUtils.getString(agent, "level").trim())
									&& "经销商".equals(MapUtils.getString(agent, "clname").trim())){
								resultMap.put("result",false);
								resultMap.put("msg","该公司已完成注册，无需提交申请。");
								return resultMap;
							}
						} else {
							if(!"经销商".equals(MapUtils.getString(agent, "clname").trim())){
								resultMap.put("result",false);
								resultMap.put("msg","该公司已完成注册，无需提交申请。");
								return resultMap;
							}
						}
					}
				}
				if("一级经销商".equals(level)){
					for (Map<String, Object> agent : simsList) {
						if (!"一级".equals(MapUtils.getString(agent, "level"))) {
							resultMap.put("result",true);
							return resultMap;
						}
					}
				}
				simsMap.put("bu", abridge);
				simsList = qiangsDao.getSimsByMap(simsBean, simsMap);
				//相同产品线判断
				if (CollectionUtils.isNotEmpty(simsList)) {
					boolean isOK = false;
					for (Map<String, Object> agent : simsList) {
						if ("红色预警".equals(MapUtils.getString(agent, "dd"))) {
							resultMap.put("result",false);
							resultMap.put("msg","该公司为严重红色预警经销商，不能申请；如有任何问题，请联系合规部。");
							return resultMap;
						} else {
							String status = jetFlowDdJy(MapUtils.getString(agent, "开始时间"), MapUtils.getString(agent, "结束时间"));
							/*if(Strings.isEmpty(status)){
								resultMap.put("result",false);
								resultMap.put("msg","DD状态、DD_Date、DDDate_End为空不允许提交!");
								return resultMap;
							} else */
							if(status.equals("DD有效")){
								isOK = true;
							}
						}
					}
					if(isOK){
						resultMap.put("result",false);
						resultMap.put("msg","系统中已存在该经销商账户,是当前产品线,DD有效;不允许提交;请联系渠道运营部门!");
						return resultMap;
					} else {
						resultMap.put("result",false);
						resultMap.put("msg","请发起renew DD经销商申请流程!");
						return resultMap;
					}
				} else {
					simsMap.put("notBU", "");
					simsList = qiangsDao.getSimsByMap(simsBean, simsMap);
					if (CollectionUtils.isNotEmpty(simsList)) {
						boolean isOK = false;
						for (Map<String, Object> agent : simsList) {
							if ("红色预警".equals(MapUtils.getString(agent, "dd"))) {
								resultMap.put("result",false);
								resultMap.put("msg","该公司为严重红色预警经销商，不能申请；如有任何问题，请联系合规部。");
								return resultMap;
							} else {
								String status = jetFlowDdJy(MapUtils.getString(agent, "开始时间"), MapUtils.getString(agent, "结束时间"));
								/*if(Strings.isEmpty(status)){
									resultMap.put("result",false);
									resultMap.put("msg","DD状态、DD_Date、DDDate_End为空不允许提交!");
									return resultMap;
								} else */
								if(status.equals("DD有效")){
									isOK = true;
								}
							}
						}
						if(isOK){
							resultMap.put("result",false);
							resultMap.put("msg","该公司DD有效，请在Fishflow中提交增加产品线申请!");
							return resultMap;
						}
					}
				}
			}
		}
		resultMap.put("result",true);
		return resultMap;
	}
	
	/**
	 * 校验通过后创建经销商账号 发起邮件
	 * @param formBean
	 * @param formDataMasterBean
	 * @param formdata
	 * @param formData
	 * @throws Exception
	 */
	public void CreateDistributor(FormBean formBean,FormDataMasterBean formDataMasterBean) throws Exception {
		V3xOrgPost post = syncOrgDao.getPostByCode(Constants.DEFAULT_ACCOUNT_ID, "code", Constants.DD_DEFAULTCODE);
		V3xOrgLevel level = syncOrgDao.getLevelByCode(Constants.DEFAULT_ACCOUNT_ID, "code", Constants.DD_DEFAULTCODE);
		V3xOrgDepartment dept = syncOrgDao.getDeptByCode(Constants.DEFAULT_ACCOUNT_ID, "code", Constants.DD_DEFAULTCODE);
		V3xOrgDepartment dept2 = syncOrgDao.getDeptByCode(Constants.DEFAULT_ACCOUNT_ID, "code", Constants.DEPTCODE);
		OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
		OrgManagerDirect OrgManagerDirect = (OrgManagerDirect) AppContext.getBean("orgManagerDirect");
		V3xOrgMember v3m = new V3xOrgMember();
		v3m.setIdIfNew();
		// 邮箱@前面一串 年月日六位数 加三个随机数
		v3m.setCode("J" + RandomCode.userCode());
		v3m.setAddress("");
		v3m.setCreateTime(new Date());
		v3m.setEnabled(true);// 是否启用
		v3m.setExternalType(1);// 外部类型
		v3m.setI18nNameId(null);
		v3m.setIdIfNew();// 新的id
		v3m.setIsAdmin(false);// 是否管理员
		v3m.setIsAssigned(true);// 是否被指派
		v3m.setIsDeleted(false);// 是否删除
		v3m.setIsInternal(false);// 是否内部人员
		v3m.setIsLoginable(true);// 是否可登陆
		v3m.setIsValid(true);// 是否有效
		v3m.setIsVirtual(false);
		V3xOrgPrincipal v3xOrgPrincipal = new V3xOrgPrincipal();
		v3xOrgPrincipal.setMemberId(v3m.getId());
		v3xOrgPrincipal.setLoginName(v3m.getCode());
		v3xOrgPrincipal.setPassword(RandomCode.getStringPwd(8));
		v3m.setV3xOrgPrincipal(v3xOrgPrincipal);
		v3m.setName(getString(formDataMasterBean, formBean, "经销商名称"));// 名称
		v3m.setOrgLevelId(-1L);// 职位级别
		//v3m.setOrgLevelId(level.getId());// 职位级别
		v3m.setOrgAccountId(dept.getOrgAccountId());// 组织账户ID
		v3m.setOrgDepartmentId(dept.getId());//
		v3m.setOrgPostId(-1L);// 组织职位ID
		//v3m.setOrgPostId(post.getId());// 组织职位ID
		v3m.setPrimaryLanguange("zh_CN");// 语言
		v3m.setReporter(13301212121L);//设置通讯
		v3m.setSortId(null);// 排序编号
		v3m.setState(1);// 状态
		v3m.setState(1);
		v3m.setStatus(1);
		v3m.setType(1);// 类型
		v3m.setUpdateTime(new Date());// 修改时间
		v3m.setVisitor(false);// 是否访客
		v3m.setExternalType(0);
		v3m.setProperty("emailaddress", getString(formDataMasterBean, formBean, "email_经销商邮箱")==null?getString(formDataMasterBean, formBean, "经销商邮箱"):getString(formDataMasterBean, formBean, "email_经销商邮箱"));
		OrgManagerDirect.addMember(v3m);
		//新增工作范围
		 List relList = new ArrayList();    
		  V3xOrgRelationship rel = new V3xOrgRelationship();
		     rel.setKey("External_Workscope");
		     rel.setSortId(v3m.getSortId());
		     rel.setSourceId(v3m.getId());
		     rel.setObjective0Id(dept2.getId());
		     rel.setOrgAccountId(v3m.getOrgAccountId());
		     rel.setObjective5Id("Department");
		     relList.add(rel);
		     OrgManagerDirect.addOrgRelationships(relList);

		log.info("KKDD:经销商账号======================" + v3m.getLoginName());
		log.info("KKDD:经销商密码======================" + v3m.getPassword());
		// 将id回写到经销商ID字段
		FormFieldBean field = formBean.getFieldBeanByDisplay("经销商");// 控件名
		formDataMasterBean.addFieldValue(field.getName(), v3m.getId());// vertifyCode人员id

		log.info("DDKK:开始发起邮件================================");
		String jxsemail = v3m.getEmailAddress();
		String http = SystemEnvironment.getInternetSiteURL();
		// Long DDID=0L;
		field = formBean.getFieldBeanByDisplay("DD专员");// 控件名
		Long ddId = Long.valueOf(formDataMasterBean.getFieldValue(field.getName()).toString());
		V3xOrgMember ddMember = orgManager.getMemberById(ddId);
		String ddemail = ddMember.getEmailAddress();
		String xssqemail = getString(formDataMasterBean, formBean, "销售申请人邮箱");
		String xsjlemail = getString(formDataMasterBean, formBean, "销售申请人直属经理邮箱");
//		String ddemail = MapUtils.getString(formdata, "DD专员邮箱");	
		String rcmemail = getString(formDataMasterBean, formBean, "RCM邮箱");

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String s = sdf.format(d);
		List<String> list = new ArrayList<String>();
		if(Strings.isNotEmpty(xsjlemail)){
			list.add(xsjlemail+"@its.jnj.com");
		}
		if(Strings.isNotEmpty(xssqemail)){
			list.add(xssqemail+"@its.jnj.com");
		}
		//dd renew推送经理邮件
		if(Strings.isEmpty(xsjlemail) && Strings.isEmpty(xssqemail)){
			xsjlemail = getString(formDataMasterBean, formBean, "经理邮箱列表");
			if(Strings.isNotEmpty(xsjlemail)){
				for(String jxs: xsjlemail.split(";")){
					if(Strings.isNotEmpty(jxs)){
						list.add(jxs);
					}
				}
			}
		}
		if(Strings.isNotEmpty(ddemail)){
			list.add(ddemail);
		}
		if(Strings.isNotEmpty(rcmemail)){
			list.add(rcmemail);
		}
		if(Strings.isNotEmpty(jxsemail)){
			for(String jxs: jxsemail.split(";")){
				if(Strings.isNotEmpty(jxs)){
					list.add(jxs);
				}
			}
		}
		String ddmail = ddMember.getName() + "	" + ddMember.getOfficeNum()
				+ "	" + ddMember.getEmailAddress();
		Date endDate = null;
		if(formBean.getFieldBeanByDisplay("DD通过日期") != null){
			endDate = getDate(formDataMasterBean, formBean, "DD通过日期");
		}
		ddupdateHcc.sendMail(list, ddmail, endDate, v3m);
		/*MessageMailManager messageMailManager = (MessageMailManager) AppContext.getBean("messageMailManager");
		boolean sendMessageByMail = messageMailManager.sendMessageByMail(list, "DD填报资料账户已开通",
				v3m.getLoginName() + "您好" + "<br><br>" + "系统为您创建好了账号,如下:" + "<br>" + "登陆名:" + v3m.getCode()
						+ "<br><br>" + "具体操作手册,如下:" + "<br>"
						+ "<br><br>" + "请登陆iChannel网站" + http + "<br><br>" + "密码在经销商的邮件里" + "<br><br>"
						+ "强生（上海）医疗器材有限公司" + "<br><br>" + s,
				new Date());
		messageMailManager.sendMessageByMail(jxsemail, "DD填报资料账户已开通(密码)",
						"您好," + "<br><br>" + "系统为您创建好了密码,如下:" + "<br>" + "密码:" + v3m.getPassword()
						+ "<br><br>" + "请登陆iChannel网站" + http + "<br><br>" + "强生（上海）医疗器材有限公司" + "<br><br>" + s,
				new Date());*/
		log.info("DDKK:邮件已发送================================");
	}
	
	public Map<String,Object> HccPaiban(FormBean formBean, FormDataMasterBean formDataMasterBean) throws Exception {
		FormTableBean formData = formBean.getMasterTableBean();
		//获取DD经销商底表bean
		log.info("DDKK:开始排班 将ID填入DD专员字段");
		FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
		FormBean formByFormCode3 = formApi4Cap4.getFormByFormCode(CodeForPaiban);
		FormTableBean masterTableBean3 = formByFormCode3.getMasterTableBean();
		StringBuilder HccPaiban = new StringBuilder("select id, "+masterTableBean3.getTableName()+"."+masterTableBean3.getFieldBeanByDisplay("人员姓名").getName()+" as member ,");
		HccPaiban.append(masterTableBean3.getTableName()+"."+masterTableBean3.getFieldBeanByDisplay("创建日期时间").getName()+" as shijian");
		HccPaiban.append(" from "+masterTableBean3.getTableName());
		HccPaiban.append(" order by "+masterTableBean3.getFieldBeanByDisplay("创建日期时间").getName()+" ASC");
		List<Map> HccPai = JdbcAgentUtil.doQuery(HccPaiban.toString(), null);
		Map map2 = HccPai.get(0);
		FormFieldBean field = formData.getFieldBeanByDisplay("DD专员");// 控件名
		formDataMasterBean.addFieldValue(field.getName(), MapUtils.getLong(map2, "member"));//vertifyCode人员id
		log.info("DDKK:完成排班 ID已填入DD专员字段"+"控件名是"+field.getName()+"ID是"+MapUtils.getLong(map2, "member"));
		OrgManager orgManager = (OrgManager) AppContext.getBean("orgManager");
		V3xOrgMember vmember = orgManager.getMemberById(MapUtils.getLong(map2, "member"));
		if(Strings.isNotEmpty(vmember.getOfficeNum())){
			field = formData.getFieldBeanByDisplay("DD专员电话");// 控件名
			formDataMasterBean.addFieldValue(field.getName(), vmember.getOfficeNum());
		}
		if(Strings.isNotEmpty(vmember.getEmailAddress())){
			field = formData.getFieldBeanByDisplay("DD专员邮箱");// 控件名
			formDataMasterBean.addFieldValue(field.getName(), vmember.getEmailAddress());
		}
		
		Long memberid = MapUtils.getLong(map2, "id");
		Map<String, Object> param = new HashMap<String, Object>();
		
		FormDataMasterBean findDataById = formApi4Cap4.findDataById(memberid,formByFormCode3, null);
		param.put("创建日期时间", new Date());
		String updateFormData = kkBaseFormManager.updateFormData(CodeForPaiban, findDataById, param);
		
		Map<String,Object> dd = new HashMap<>();
		dd.put("UserForDd", MapUtils.getString(map2, "member"));
		return dd;
	}
	/**
	 * 根据时间校验DD是否有效
	 * @param DD_Status DD状态
	 * @param _DD_Date	DD开始时间
	 * @param _DD_DATE_END	DD结束时间
	 * @return
	 */
	public  String jetFlowDdJy(String _DD_Date, String _DD_DATE_END) {
		if(StringUtils.isNotBlank(_DD_Date)&&StringUtils.isNotBlank(_DD_DATE_END)){//如果均不为空，执行判断，返回内容，若有空值向下执行
			Date  DD_DATE_END=CoreUtils.parseDate(_DD_DATE_END,  CoreUtils.DAYTIME_PARSE_PATTERNS) ;
			if (DD_DATE_END.after(new Date())){
				return "DD有效";
			}
			return "DD无效";
		} else if(StringUtils.isNotBlank(_DD_Date)){
			Date  DD_Date=getYear(CoreUtils.parseDate(_DD_Date, CoreUtils.DAYTIME_PARSE_PATTERNS));
			if (DD_Date.before(new Date()) ){
				return "DD无效";
			}
			return "DD有效";
		}  else if(StringUtils.isNotBlank(_DD_DATE_END)){
			Date  DD_DATE_END= CoreUtils.parseDate(_DD_DATE_END, CoreUtils.DAYTIME_PARSE_PATTERNS) ;
			if (DD_DATE_END.after(new Date()) ){
				return "DD有效";
			}
			return "DD无效";
		}
		return "";
	}
	
	public static Date getYear(Date today) {
		Calendar cale = Calendar.getInstance();
		cale.setTime(today);
		cale.add(Calendar.YEAR, 3);
		return cale.getTime();

	}
	
	/**wtt
	 * 手机号、邮箱格式校验
	 * @param formBean
	 * @param formDataMasterBean
	 */
	public String mailAndPhoneCheck(FormBean formBean,FormDataMasterBean formDataMasterBean){
		Map<String, FormFieldBean>  fieldMap=formDataMasterBean.getFormTable().getFieldMap4Display();
		String result="";
		for (Map.Entry <String, FormFieldBean>  entry :fieldMap.entrySet()) {
			if(entry.getKey().contains("email_")){
				String checkStr=getString(formDataMasterBean, formBean, entry.getKey());
				if(StringUtils.isNotEmpty(checkStr)){
					//邮件校验
					 Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配  
			         Matcher m = p.matcher(checkStr); 
					  if(!m.matches()){
						  result=entry.getKey()+"字段，邮箱格式不正确!"; 
						  log.info("ddkk邮箱校验出错："+checkStr);
						  return result;
					  }
				}
				
			}else if(entry.getKey().contains("phone_")){
				String checkStr=getString(formDataMasterBean, formBean, entry.getKey());
				if(StringUtils.isNotEmpty(checkStr)){//手机号校验
					  Pattern p = null;
					  Matcher m = null;
					  boolean b = false;
					  p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号
					  m = p.matcher(checkStr);
					  b = m.matches();
					  if(!b){
						  result=entry.getKey()+"字段，手机号码格式不正确!"; 
						  log.info("ddkk手机号校验出错："+checkStr);
						  return result;
					  }
				}
			}
		}
		return "";
	}
	
	private String getString(FormDataMasterBean formDataMasterBean, FormBean formBean, String display){
		try{
			return formDataMasterBean.getFieldValue(formBean.getMasterTableBean()
					.getFieldBeanByDisplay(display).getName()).toString();
		} catch(Exception e) {
			return null;
		}
	}
	
	private Date getDate(FormDataMasterBean formDataMasterBean, FormBean formBean, String display){
		try{
			return (Date) formDataMasterBean.getFieldValue(formBean.getMasterTableBean()
					.getFieldBeanByDisplay(display).getName());
		} catch(Exception e) {
			return null;
		}
	}
	
	public void ddStopLog(FormBean formBean, FormDataMasterBean formDataMasterBean,String nodePolicy) throws Exception {
		if("so审核".equals(nodePolicy)){
			Map<String, Object> param=new HashMap<String, Object>();
			String ddName=formBean.getMasterTableBean().getFieldBeanByDisplay("企业征信")==null?formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("经销商名称").getName()).toString()
					:StringUtils.substringBefore(formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("企业征信").getName()).toString(), "|");
			param.put("经销商名称",ddName);
			param.put("基本信息", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("是否通过-公司信息").getName()));
			param.put("公司资质", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("是否通过-公司资质").getName()));
			param.put("订货信息", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("是否通过-订货信息").getName()));
			param.put("关键人员简历", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("是否通过-人员简历").getName()));
			param.put("关联公司申明", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("是否通过-公司申明").getName()));
			param.put("DD专员", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("DD专员").getName()));
			param.put("退单时间", new Date());
			kkBaseFormManager.saveUnflowData(Constants.soformCode, Constants.createUser, param); 
		}else if("HCC".equals(nodePolicy)){
			Map<String, Object> param=new HashMap<String, Object>();
			String ddName=formBean.getMasterTableBean().getFieldBeanByDisplay("企业征信")==null?formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("经销商名称").getName()).toString()
					:StringUtils.substringBefore(formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("企业征信").getName()).toString(), "|");
			param.put("经销商名称", ddName);
			param.put("退单时间", new Date());
			param.put("HCC", formDataMasterBean.getFieldValue(formBean.getMasterTableBean().getFieldBeanByDisplay("HCC").getName()));
			List<FormTableBean> subTableBean=formBean.getSubTableBean();
			FormTableBean sonformTableBean=new FormTableBean();
			for (FormTableBean formTableBean : subTableBean) {
				if("丨SO上传所有附件丨".equals(formTableBean.getDisplay())){
					sonformTableBean=formTableBean;
				}
			}
			List<FormDataSubBean> formDataSubBean =formDataMasterBean.getSubData(sonformTableBean.getTableName());
			List<Map<String, Object>> sonparam=new ArrayList<Map<String,Object>>();
			for (FormDataSubBean formDataSubBean2 : formDataSubBean) {
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("退单说明", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("退单说明").getName()));
				map.put("文件类型", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("文件类型").getName()));
				map.put("补充文件名", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("其他文件").getName()));
				map.put("材料缺少", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("材料缺少").getName()));
				map.put("内容逻辑不合理", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("内容逻辑不合理").getName()));
				map.put("内容缺失或填写错误", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("内容缺失或填写错误").getName()));
				map.put("红色预警", formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("红色预警").getName()));
				map.put("其他",formDataSubBean2.getFieldValue(sonformTableBean.getFieldBeanByDisplay("其他").getName()));
				sonparam.add(map);
			}
			param.put("退单详情", sonparam);
			kkBaseFormManager.saveUnflowData(Constants.hccformCode, Constants.createUser, param);
		}
	}
}
